/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.casemain.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseSurveyPeopleInfoDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseSurveyPeopleInfoQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseSurveyPeopleInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseSurveyPeopleInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseHandlePersonRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseSurveyPeopleInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseHandlePersonRecordVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseSurveyPeopleBasicInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseSurveyPeopleInfoVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 描述:  case_survey_people_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月14日
 */
@Service
public class CaseSurveyPeopleInfoServiceImpl extends MyBatisServiceSupport implements ICaseSurveyPeopleInfoService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseSurveyPeopleInfoServiceImpl.class);

    @Autowired
    private CaseSurveyPeopleInfoMapper caseSurveyPeopleInfoMapper;
    @Resource
    private ICaseHandlePersonRecordService caseHandlePersonRecordService;

    @Override
    public String saveSurveyPeopleInfo(CaseSurveyPeopleInfoDto dto) {
        String uuid = StringUtils.uuid();
        CaseSurveyPeopleInfo caseSurveyPeopleInfo = new CaseSurveyPeopleInfo();
        BeanUtils.copyProperties(dto, caseSurveyPeopleInfo);
        caseSurveyPeopleInfo.setId(uuid);
        caseSurveyPeopleInfo.setCreateTime(LocalDateTime.now());
        caseSurveyPeopleInfoMapper.insertSelective(caseSurveyPeopleInfo);
        return uuid;
    }

    @Override
    public Integer updateSurveyPeopleInfo(CaseSurveyPeopleInfoDto dto) {
        CaseSurveyPeopleInfo caseSurveyPeopleInfo = new CaseSurveyPeopleInfo();
        BeanUtils.copyProperties(dto, caseSurveyPeopleInfo);
        Example example = new Example(CaseSurveyPeopleInfo.class);
        example.createCriteria().andEqualTo("id",caseSurveyPeopleInfo.getId());
        return caseSurveyPeopleInfoMapper.updateByExampleSelective(caseSurveyPeopleInfo, example);
    }

    @Override
    public CaseSurveyPeopleInfoVo getSurveyPeopleInfo(CaseSurveyPeopleInfoDto dto) {
        CaseSurveyPeopleInfoVo caseSurveyPeopleInfoVo = new CaseSurveyPeopleInfoVo();
        CaseSurveyPeopleInfo caseSurveyPeopleInfo = caseSurveyPeopleInfoMapper.selectByPrimaryKey(dto.getId());
        BeanUtils.copyProperties(caseSurveyPeopleInfo,caseSurveyPeopleInfoVo);
        return caseSurveyPeopleInfoVo;
    }

    @Override
    public CaseSurveyPeopleBasicInfoVo getSurveyPeopleInfoByDocument(CaseSurveyPeopleInfoQueryDto queryDto) {
        CaseSurveyPeopleBasicInfoVo caseSurveyPeopleInfoVo = new CaseSurveyPeopleBasicInfoVo();
        Example example=new Example(CaseSurveyPeopleInfo.class);
        // 目前只取一个被询问人信息
        example.createCriteria().andEqualTo("caseId",queryDto.getCaseId());
        example.orderBy("createTime").desc();
        List<CaseSurveyPeopleInfo> caseSurveyPeopleInfoList = caseSurveyPeopleInfoMapper.selectByExample(example);
        if (ObjectUtil.isNotEmpty(caseSurveyPeopleInfoList) && caseSurveyPeopleInfoList.size()>0){
            BeanUtils.copyProperties(caseSurveyPeopleInfoList.get(0),caseSurveyPeopleInfoVo);
            List<CaseHandlePersonRecordVo> handlePersons = caseHandlePersonRecordService.getCaseHandlePersonRecordListByCaseId(queryDto.getCaseId());
            caseSurveyPeopleInfoVo.setHandlePersonInfoList(handlePersons);
           return caseSurveyPeopleInfoVo;
        }
        return null;
    }
}
